package com.dayouzi.crawler_monitor.utils;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.net.MalformedURLException;
import java.net.URL;

public class UrlUtils {

    public static String joinUrl(String curl, String file) {

        try {
            URL url = new URL(new URL(curl), file);
            String q = url.toExternalForm();
            if (q.indexOf("#") != -1)
                q = q.replaceAll("^(.+?)#.*?$", "$1");
            return q;
        } catch (MalformedURLException e) {
            return null;
        }
    }

    /**
     * 处理页面当中标签的href访问路径，可以将相对路径修改为绝对路径
     * @param info html网页
     * @param url 域名
     * @return
     */
    public static String completeHref(String info, String url) {
        if (info.equals("")) {
            return "";
        }
        Document doc = Jsoup.parse(info);
        //处理<a>标签中的链接
        Elements eles = doc.select("a");
        for (Element ele : eles) {
            String href = ele.attr("href").trim();
            if (!href.contains("http")) {
                href = joinUrl(url, href);
                ele.attr("href", href);
            }
        }

        //处理<iframe>标签中的链接
        Elements eles2 = doc.select("iframe");
        for (Element ele : eles2) {
            String src = ele.attr("src").trim();
            if (!src.contains("http")) {
                src = joinUrl(url, src);
                ele.attr("src", src);  
            }
        }

        //处理<embed>标签中的链接
        Elements eles3 = doc.select("embed");
        for (Element ele : eles3) {
            String src = ele.attr("src").trim();
            if (!src.contains("http")) {
                src = joinUrl(url, src);
                ele.attr("src", src);  
            }
        }

        //处理<img>标签中的链接
        Elements eles4 = doc.select("img");
        for (Element ele : eles4) {
            String src = ele.attr("src").trim();
            if (!src.contains("http")) {
                src = joinUrl(url, src);
                ele.attr("src", src);  
            }
        }

        return doc.select("body").outerHtml();
    }



}
